package com.game.betLottery.api;

import java.util.Date;
import java.util.List;

import com.eova.common.constant.ConstantValue;
import com.eova.common.id.Sequence;
import com.eova.common.until.StringUtil;
import com.game.betLottery.model.Bet;
import com.game.betLottery.model.GameRuleInfo;
import com.game.betLottery.model.Lottery;
import com.game.hall.model.GameUser;
import com.game.message.model.GameUserMessage;
import com.game.my.model.Account;
import com.jfinal.core.Controller;

public class BetOrLotteryApi extends Controller {
	// 下注
	public void bet() {

		String roomId = getPara("roomId");// 房间的id
		String nickname = getPara("nickname");// 用户的昵称
		String ruleId = getPara("ruleId");// 游戏规则的id
		String unitprice = getPara("unitprice");
		// TODO 根据用户名获取用户的信息（这个将来要用openId）
		GameUser gu = GameUser.dao.getGameUserByNickName(nickname);

		double incomeOrExpenditure = 0;
		Date createTime = new Date(); // 创建时间
		Bet bet = getModel(Bet.class);
		String id = Sequence.nextId();
		bet.set("id", id);
		bet.set("userId", gu.get("id"));
		bet.set("gameId", 0);
		bet.set("ruleId", ruleId);
		bet.set("roomId", roomId);
		bet.set("inputMoney", unitprice);
		bet.set("isWinning", ConstantValue.EOVA_NO);
		bet.set("incomeOrExpenditure", incomeOrExpenditure);
		bet.set("createTime", createTime);
		bet.set("isOver", ConstantValue.EOVA_NO);
		boolean flag = bet.save();
		setAttr("flag", flag);
		setAttr("id", id);
		renderJson();
	}

	public void LotteryApi() {
		// int gameId = Integer.valueOf(getPara("gameId"));
		int touMoney = 10;// 赔了多少钱
		int singMoney = 10;// 单价
		int rolId = 1;// 规则id
		int recoMoney = GameRuleInfo.dao.findOdds(1);// 赔率
		recoMoney = singMoney * recoMoney;// 单价乘赔率
		int flag = Lottery.dao.UpGameDetail(touMoney, recoMoney, rolId);
		renderJson(flag);
	}

	public void getInfos() {
		String roomId = getPara("roomId");// 房间的id
		List<Bet> infos = Bet.dao.getInfos(roomId);
		setAttr("infos", infos);
		renderJson();
	}

	/**
	 * 
	 * 描述:得到开奖后的用户的集合 作者:gyp
	 */
	public void getKJInfos() {
		String roomId = getPara("roomId");// 房间的id
		int resultKJ = StringUtil.buildNumber();
		Bet.dao.updateWin(roomId, resultKJ);

		List<Bet> infos = Bet.dao.getInfos(roomId);
		Bet info = Bet.dao.getInfo(roomId, resultKJ);

		// 向金额表中添加信息
		Double balance = 0d;
		Long userId = info.get("userId");
		Account account = Account.dao.getAcount(userId);
		if (null == account) {
			balance = 0.0;
		} else {
			balance = account.getDouble("balance");
		}
		Account acc = getModel(Account.class);
		int odds = info.getInt("odds");
		int unitPrice = info.getInt("unitPrice");
		acc.set("balance", balance + odds * unitPrice);
		acc.set("userId", userId);
		acc.set("createTime", new Date());
		boolean flag = acc.save();

		// 获胜者发送信息存储
		GameUserMessage gum = getModel(GameUserMessage.class);
		gum.set("id", Sequence.nextId());
		gum.set("userId", userId);
		gum.set("roomId", roomId);
		gum.set("money", balance + odds * unitPrice);
		gum.set("winmoney", odds * unitPrice);
		gum.set("isSystem", ConstantValue.EOVA_NO);
		gum.set("createTime", new Date());
		gum.save();

		setAttr("infos", infos);
		setAttr("info", info);
		setAttr("flag", ConstantValue.EOVA_SUCCESS);

		renderJson();
	}

	/**
	 * 描述:该房间的游戏结束 作者:gyp
	 */
	public void gameOver() {
		String roomId = getPara("roomId");// 房间的id
		Bet.dao.gameOver(roomId);
		setAttr("result", ConstantValue.EOVA_SUCCESS);
		renderJson();
	}

}